//
//  LogListener.m
//  Cidney
//
//  Created by Chris Karr on 1/11/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import <AddressBook/AddressBook.h>

#import "LogListener.h"
#import "Dispatcher.h"
#import "Caller.h"

#define PERSON_ID @"Person ID"
#define SAVED_CALLS @"Saved Calls"

@implementation LogListener

- (void) load
{
	NSArray * savedCalls = [[NSUserDefaults standardUserDefaults] valueForKey:SAVED_CALLS];
	
	ABAddressBook * ab = [ABAddressBook sharedAddressBook];
	
	if (savedCalls != nil)
	{
		for (NSDictionary * call in savedCalls)
		{
			Caller * newCall = [Caller dictionary];
		
			for (NSString * key in [call allKeys])
			{
				if (![key isEqualToString:PERSON_ID])
					[newCall setValue:[call valueForKey:key] forKey:key];
				else
				{
					ABRecord * person = [ab recordForUniqueId:[call valueForKey:key]];
					
					if (person != nil)
						[newCall setValue:person forKey:PERSON_KEY];
				}
			}
			
			[calls addObject:newCall];
		}
	}
}

- (void) save
{
	NSMutableArray * savedCalls = [NSMutableArray array];

	for (NSDictionary * call in [calls arrangedObjects])
	{
		NSMutableDictionary * callDict = [NSMutableDictionary dictionary];
		
		for (NSString * key in [call allKeys])
		{
			if (![key isEqualToString:PERSON_KEY])
				[callDict setValue:[call valueForKey:key] forKey:key];
			else
			{
				ABPerson * person = [call valueForKey:key];
				
				[callDict setValue:[person uniqueId] forKey:PERSON_ID];
			}
		}
		
		[savedCalls addObject:callDict];
	}
	
	[[NSUserDefaults standardUserDefaults] setValue:savedCalls forKey:SAVED_CALLS];
}

- (void) save:(NSNotification *) theNote
{
	[self save];
}

- (void) awakeFromNib
{
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(update:) name:INCOMING_CALL object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(save:) name:NSApplicationWillTerminateNotification object:nil];

	[calls setSortDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES]]];

	[self load];
}

- (void) update: (NSNotification *) msg
{
	NSDictionary * userInfo = [msg userInfo];
	
	ABPerson * person = [userInfo valueForKey:PERSON_KEY];
	NSString * number = [userInfo valueForKey:NUMBER_KEY];
	NSString * name = [userInfo valueForKey:NAME_KEY];

	Caller * call = [Caller dictionary];
	[call setValue:[NSDate date] forKey:@"date"];
	[call setValue:number forKey:@"number"];
	[call setValue:name forKey:@"name"];
	[call setValue:person forKey:PERSON_KEY];

	NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
	
	[calls setSortDescriptors:[NSArray arrayWithObject:sort]];
	
	[sort release];
	
	if ([[calls arrangedObjects] count] > 0)
	{
		Caller * lastCall = [[calls arrangedObjects] objectAtIndex:0];

		if (lastCall != nil)
		{
			if (abs ([[lastCall valueForKey:@"date"] timeIntervalSinceNow]) < 10)
				[calls removeObject:lastCall];
		}
	}
		
	[calls addObject:call];

	[self save];
}

@end
